<?php
/**
 * Removes a file
 * 
 * @author Sean
 * @return prints out the result
 */
 
include ("connect.php");
include ("config.php");

$GoogleID = $_GET['googleId'];
$GoogleID = mysql_real_escape_string($GoogleID);

$SessionID = $_GET['sessionId'];
$SessionID = mysql_real_escape_string($SessionID);

$fileToRemove = $_GET['fileToRemove'];
$fileToRemove = mysql_real_escape_string($fileToRemove);


$result = mysql_query("SELECT sessionID FROM users WHERE GoogleID = '" . $GoogleID . "'");
while ($row = mysql_fetch_array($result)) {
   $DB_SessionID = $row["sessionID"];
}

if($SessionID == $DB_SessionID)
{	
	// If the file exist, remove it and the galleryimage and the thumbimage
	if(file_exists($pathToUsersFiles.$GoogleID."/".$fileToRemove))
	{
		$res = unlink($pathToUsersFiles.$GoogleID."/".$fileToRemove);
		
		if( file_exists($pathToUsersFiles.$GoogleID."/thumbs/".$fileToRemove) ){
			unlink($pathToUsersFiles.$GoogleID."/thumbs/".$fileToRemove);
		}
		if( file_exists($pathToUsersFiles.$GoogleID."/gallery/".$fileToRemove) ){
			unlink($pathToUsersFiles.$GoogleID."/gallery/".$fileToRemove);
		}
		
		if($res == true){
			header('Access-Control-Allow-Origin: *');
			echo "File ".$fileToRemove." was removed!!";
		}
		else{
			header('Access-Control-Allow-Origin: *');
			echo "ERROR, File ".$fileToRemove." was NOT removed! ";
		}
	}
	else{
		echo "ERROR: File does not exist.";
	}	
}
else{
	header('Location: http://sean.servern.org/webdrop/Webdrop.html?#page=Login');
}
?>